package com.pwc.conversionTool.Manager;



import javax.servlet.ServletContext;

import org.dom4j.DocumentException;
import org.springframework.context.support.StaticApplicationContext;

import com.eviware.soapui.model.iface.Request.SubmitException;
import com.pwc.conversionTool.soapUI.SoapUIClient;

public class SoapUIManager {
	
	private static SoapUIClient client=null; 
	
	private static String url=null;
	
	
	private SoapUIManager()
	{
		
	}
	
	public static String  sendSoap(ServletContext context,String operation,String data) throws DocumentException, SubmitException
	{
		String resultString="";
		if(isInitial())
		{
			client.generateWSDLRequest(operation, data);
			SettingSecurity(context);
			resultString=	client.doSubmit(operation, data);
		}
		
		return resultString;
	}
	
	public static void settingEndpoint(String url){
		if(!url.equals(SoapUIManager.url))
		{
			SoapUIManager.url=url;
			client=new SoapUIClient();
			client.initialProject(SoapUIManager.url);
		}
	}
	
	
	private static void SettingSecurity(ServletContext context) throws DocumentException{
		if(isInitial())
		{
			String userName=XMLConfigImpl.initial(context).getUserName();
			String password=XMLConfigImpl.initial(context).getPassword();
			String passwordType=XMLConfigImpl.initial(context).getPasswordType();
			if(userName.length()>0&&password.length()>0&&passwordType.equals("PasswordText"))
			{
				client.setSecurity(userName, password, passwordType);
			}else if(userName.length()>0&&password.length()>0) {
				client.setSecurity(userName, password);
			}
			
		}
	}
	

	public static boolean isInitial()
	{
		return client!=null;
	}

	public static SoapUIClient getSoapUIClient()
	{
		return client;
	}
	
	public static String getUrl()
	{
		return url;
	}
	
	
}
